//引入
const db = require('./utils/db');
const express = require('express');
const jwt = require('jsonwebtoken');
const loginRouter = require('./router/login');
const mainRouter = require('./router/main');
const balanceRouter = require('./router/balance');
const adminRouter = require('./router/admin');
const accountRouter = require('./router/schoolAccount');
const studentAccountRouter = require('./router/studentAccount');
const roleRouter = require('./router/roleManagement');
const associatedRouter = require('./router/associatedUsers');
const noticePayRouter = require('./router/noticePayment');
const classRouter = require('./router/classManagement');
const studentRouter = require('./router/studentManagement');
const teacherRouter = require('./router/teacherManagement');

//搭建服务
const app = express();

//配置post
app.use(express.json()); //数据格式转JSON
app.use(express.urlencoded({ extended: false })); //用from表单格式

// cors的配置
app.all('*', function (req, res, next) {
  /* 设置响应头，允许所有网站都可以请求 */
  res.header('Access-Control-Allow-Origin', '*');
  /* 设置我们的请求方式可以有以下这些 */
  res.header('Access-Control-Allow-Methods', 'GET, POST, PUT, DELETE,OPTIONS');
  /* 设置响应头可以有以下这些 */
  res.header(
    'Access-Control-Allow-Headers',
    'Content-Type,Content-Length, Authorization, Accept,X-Requested-With,token'
  );
  /* 放行 */
  req.method === 'OPTIONS' ? res.status(204).end() : next();
});

// 配置token的拦截
// app.all("*", (req, res, next) => {
//     // console.log(req.headers.token);
//     // console.log(req.url);
//     if (req.url == '/login') {
//         next();
//     } else {
//         let token = req.headers.token
//         if (token) {
//             // 验证token是否正确
//             jwt.verify(token, 'web308', (err, decoded) => {
//                 if (err) {//传过来的密钥错误
//                     console.log(err);
//                     res.status(401).send({ error: 'token无效' })
//                 } else {//解析成功
//                     // console.log(decoded);
//                     /*   {
//                           data: {
//                             a_id: 1,
//                             ...
//                           },
//                           iat: 1665215077,//现在时间
//                           exp: 1665215197//过期时间2分钟
//                         } */
//                     // console.log(decoded.exp - decoded.iat);
//                     if (decoded.exp - decoded.iat > 0) {
//                         next();
//                     } else {
//                         // 时间过期了
//                         res.status(401).send({ error: "token时间过期了" })
//                     }
//                 }
//             })

//         } else {
//             res.status(401).send({ error: "没有token" })
//         }
//     }
// })

//配置路由
app.use(loginRouter);
app.use(mainRouter);
app.use(balanceRouter);
app.use(adminRouter);
app.use(accountRouter);
app.use(studentAccountRouter);
app.use(associatedRouter);
app.use(roleRouter);
app.use(noticePayRouter);
app.use(classRouter);
app.use(studentRouter);
app.use(teacherRouter);

//配置端口号
app.listen(5555);
